package pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import utils.Log4Test;

/**
 * Created by WHOover on 13.07.2014.
 */
public class FoundedProductPage extends BasePage {
    private static final By addToCompareLocator = By.xpath("//div[@class='g-i-list-compare']/a[@class='xhr lightblue']");
    private static final By compareLocator = By.className("g-i-list-compare");
    protected final String productFoundLocator = "//a[contains(text(),'%s')]";
    private static final By toCompareLocator = By.xpath("//div[@class='g-i-list-compare']/a[@class='lightblue underline']");

    public FoundedProductPage(WebDriver driver) {
        super(driver);
    }

    public boolean isOpened() {
        return driver.getCurrentUrl().equals(URL);
    }

    public void addToCompare(String item) {
        WebDriverWait wait = new WebDriverWait(driver, 25);

        Log4Test.info("Adding to compare " + item);
        wait.until(ExpectedConditions.elementToBeClickable(addToCompareLocator));
        driver.findElement(addToCompareLocator).click();
        wait.until(ExpectedConditions.textToBePresentInElement(driver.findElement(compareLocator), "Добавлен к сравнению"));
    }

    public void compareItems() {
        WebDriverWait wait = new WebDriverWait(driver,30);

        wait.until(ExpectedConditions.presenceOfElementLocated(toCompareLocator));
        driver.findElement(toCompareLocator).click();
        Log4Test.info("run compare");
    }

    public boolean isComparePageAvailable() {
        return driver.findElement(toCompareLocator).isDisplayed();
    }

    public boolean productPresent(String productName) {
        WebDriverWait wait = new WebDriverWait(driver,20);

        wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(String.format(productFoundLocator,productName))));
        return driver.findElement(By.xpath(String.format(productFoundLocator,productName))).isDisplayed();
    }
}
